<?php
	Class admin_userController Extends baseController {
		public function index()
		{
			$where = 'Quyen <> 0';
            if(isset($_SESSION['user'])){
                $user = $_SESSION['user'];
                if($user->Quyen == 1){
                    $where .= ' and Quyen <> 1';
                }
            }
			$sapxep = '';
			$by = '';
			$userModel = new user();
			$current_page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
			$pos = ($current_page -1 ) * __PAGE_SIZE;
			$lstu = $userModel->getUserByWherePaging($where, $sapxep.''.$by , $pos);
			$count = count((array)$userModel->getUserByWhere($where, $sapxep.''.$by));
			$pages = ceil($count/__PAGE_SIZE);
	
			$this->registry->template->lstUser = $lstu;
			$this->registry->template->pages = $pages;
			$this->registry->template->show('admin/user_view');
		}
		
		public function edit(){
			if(isset($_REQUEST['user_id'])){
				$user_id = $_REQUEST['user_id'];
				$userModel = new user();
				$user = $userModel->getUserByTenDangNhap($user_id);
				$this->registry->template->user = $user;
			}
			$this->registry->template->show('admin/user_edit');
		}
		
		public function save(){
			
			$user = new user();
			$user->MatKhau = md5('123456');
			$user->HoTen = $_POST['thoten'];
			$user->Email = $_POST['temail'];
			$user->DienThoai = $_POST['tdienthoai'];
			$user->DiaChi = $_POST['tdiachi'];
			$user->Quyen = $_POST['rquyen'];
			$user->TrangThai = $_POST['rtrangthai'];
			if(isset($_REQUEST['user_id'])) $user->TenDangNhap = trim($_GET['user_id']);
			else $user->TenDangNhap = trim($_POST['ttendangnhap']);
			
			//kiem tra neu ten dang nhap co khoang trang thi hien thong bao
			if ( preg_match('/\s/',$user->TenDangNhap) ){
				$this->registry->template->message_user = 'Tên đăng nhập không được có khoảng cách.';
				$this->registry->template->user = $user;
				$this->registry->template->show('admin/user_edit');
				return;	
			}
			//lay danh sach user de kiem tra ten dang nhap va email xem da co chua
			if(isset($_REQUEST['user_id'])) $lstUser = $user->getUserByWhere('TenDangNhap <> "'.trim($_GET['user_id']).'"','');
			else $lstUser = $user->getUserByWhere('','');
			if(count($lstUser)>0)
			{
				//kiem tra neu ton tai ten dang nhap do roi thi hien thong bao
				foreach($lstUser as $u){
					if(($u->TenDangNhap == $user->TenDangNhap)){
						$this->registry->template->message_user = 'Tên đăng nhập đã tồn tại.';
						$this->registry->template->user = $user;
						$this->registry->template->show('admin/user_edit');
						return;	
					}
				}
			
				//kiem tra neu ton tai email do roi thi hien thong bao
				foreach($lstUser as $u){
					if($u->Email == $user->Email){
						$this->registry->template->message_email = 'Email đã tồn tại.';
						$this->registry->template->user = $user;
						$this->registry->template->show('admin/user_edit');
						return;	
					}
				}
			}
			if(isset($_REQUEST['user_id'])){
				//$user->TenDangNhap = $_GET['user_id'];
				$rs = $user->updateUser($user);
			}
			else{
				$user->Created_Date = date('Y-m-d');
				$rs = $user->insertUser($user);
			}
			
			if($rs){
				echo '<script>alert("thành công.");</script>';
				header('location:'.__URL_BASE.'index.php?rt=admin_user');
				exit;
			}
			else{
				echo '<script>alert("Có lỗi xảy ra.");</script>';
			}
		}
        
        public function profile(){
            
            if(isset($_SESSION['user'])) $user = $_SESSION['user'];
            $this->registry->template->user = $user;
            $this->registry->template->show('admin/user_profile');
            return;
        
        }
        
        public function profile_save(){
            $user = new user();
            $user->TenDangNhap = trim($_POST['ttendangnhap']);
			$user->MatKhau = $_SESSION['user']->MatKhau;
			$user->HoTen = $_POST['thoten'];
			$user->Email = $_POST['temail'];
			$user->DienThoai = $_POST['tdienthoai'];
			$user->DiaChi = $_POST['tdiachi'];
			$user->Quyen = $_SESSION['user']->Quyen;;
			$user->TrangThai = $_SESSION['user']->TrangThai;
            //echo '<pre>'; var_dump($user); die;
            $lstUser = $user->getUserByWhere('TenDangNhap <> "'.$user->TenDangNhap.'"','');
            if(count($lstUser)>0)
			{
			
				//kiem tra neu ton tai email do roi thi hien thong bao
				foreach($lstUser as $u){
					if($u->Email == $user->Email){
						$this->registry->template->message_email = 'Email đã tồn tại.';
						$this->registry->template->user = $user;
						$this->registry->template->show('admin/user_edit');
						return;	
					}
				}
			}
            $rs = $user->updateUser($user);
            if($rs){
                //echo '123';die;
                $this->registry->template->msg = 'Sửa hồ sơ thành công';
            }
            $this->registry->template->user = $user;
            $this->registry->template->show('admin/user_profile');
        }
        
        public function change_password(){
            $this->registry->template->show('admin/user_change_pass');
        }
        
        public function change_pass_save(){
            $userModel = new user();
            $user = $_SESSION['user'];
            if($user->MatKhau != md5(trim($_POST['tpass']))){
                $this->registry->template->wrong_pass = 'Mật khẩu không đúng.';
                $this->registry->template->show('admin/user_change_pass');
                return;
            }
            $user->MatKhau = md5(trim($_POST['tpassnew']));
            $rs = $userModel->updateUser($user);
            
            if($rs){
                //echo '123';die;
                $this->registry->template->msg = 'Sửa mật khẩu thành công';
            }
            $this->registry->template->show('admin/user_change_pass');
        }
	}
?>